﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Level4RotateLetters : MonoBehaviour
{
    public List<RotateLetters> rotateLettersList = new List<RotateLetters>();
    public GameLevelItem gameLevelItem;
    // Start is called before the first frame update
    void Start()
    {
        InitRotateLetters();
    }

    void InitRotateLetters()
    {
        int count = rotateLettersList.Count;
        for (int i = 0; i < count; i++)
        {
            int tempIndex = i;
            rotateLettersList[i].btn.onClick.AddListener(() =>
            {
                SoundManager.Instance.PlayAudio(SoundType.Click.ToString(), false);
                if (GameCtrl.Instance.curGameState != GameState.Play) return;
                RotateLetter(tempIndex);
            });
        }
    }

    private void RotateLetter(int curIndex)
    {
        rotateLettersList[curIndex].AddRotation(-1);
        if (curIndex - 1 >= 0)
            rotateLettersList[curIndex - 1].AddRotation(1);
        if (curIndex + 1 <= 3)
            rotateLettersList[curIndex + 1].AddRotation(1);
    }

    public void JudgeResult()
    {
        if (GameCtrl.Instance.curGameState != GameState.Play) return;
        bool result = true;
        int count = rotateLettersList.Count;
        for(int i =0;i < count;i++)
        {
            if (!rotateLettersList[i].isRight)
                result = false;
        }
        if (result)
            gameLevelItem.TriggerResult(true);
    }
}
